package com.sambo.inspection.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.sambo.inspection.R;
import com.sambo.inspection.bean.InspectionBean;

import java.util.ArrayList;
import java.util.List;

public class TaskExpandableListAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private List<String> groupList;
    private List<ArrayList<InspectionBean>> childList;
    private LayoutInflater mInflater;

    public TaskExpandableListAdapter(Context context, List<String> groupList, List<ArrayList<InspectionBean>> childList) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(context);
        this.groupList = groupList;
        this.childList = childList;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childList.get(groupPosition).get(childPosition);
    }

    private int selectedGroupPosition = -1;
    private int selectedChildPosition = -1;

    public void setSelectedPosition(int selectedGroupPosition, int selectedChildPosition) {
        this.selectedGroupPosition = selectedGroupPosition;
        this.selectedChildPosition = selectedChildPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childList.get(groupPosition).size();
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        ChildHolder childHolder;

        if (convertView == null) {
            childHolder = new ChildHolder();
            convertView = mInflater.inflate(R.layout.item_task_child, null);
            childHolder.tvTaskCode = (TextView) convertView.findViewById(R.id.tv_task_code);
            childHolder.tvTaskName = (TextView) convertView.findViewById(R.id.tv_task_name);

            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }

        final InspectionBean bean = (InspectionBean) getChild(groupPosition, childPosition);

        childHolder.tvTaskCode.setText(bean.getCode());
        childHolder.tvTaskName.setText(bean.getName());

        return convertView;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupHolder groupHolder;
        if (convertView == null) {
            groupHolder = new GroupHolder();
            convertView = mInflater.inflate(R.layout.item_task_parent, null);
            groupHolder.tvGroupName = (TextView) convertView.findViewById(R.id.tv_task_parent_name);
            convertView.setTag(groupHolder);
        } else {
            groupHolder = (GroupHolder) convertView.getTag();
        }

        if (getGroup(groupPosition) != null) {
            groupHolder.tvGroupName.setText(getGroup(groupPosition).toString());
        } else {
            groupHolder.tvGroupName.setText("");
        }

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    static class GroupHolder {
        public TextView tvGroupName;
    }

    static class ChildHolder {
        public TextView tvTaskCode;
        public TextView tvTaskName;
    }
}
